XN.Entry = {
	extra:FWK.extra,
	urls:{
		login:"/login/memberLogin.do",
		logout:"/login/memberLogout.do",
		identity:["/login/memberIdentity.do","/login/memberRefresh.do?m=refresh","/login/memberIdentity.do?m=refresh"],
		verifycode:"/login/ImageCode?paramName=LOGIN"
	},
	emptys:[
			":: 原蜗牛通行证可直接登录！",
			":: 请输入您的密码！",
			":: 请输入验证码！"
			],
	errors:[
			"<span style='color:red'>:: 您输入的通行证格式不正确！</span>",
			"&nbsp;",
			"<span style='color:red'>:: 您输入的验证码格式不正确！</span>"
			],
	loginInfo:[":: 正在登录...",":: 登录成功..."],
	formUrl:{
		1:"my." + XN.doMain
	},
	regExp:[/^.+$/,/^.+$/,/^\d+$/],
	mailRegExp:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
	loginXML:null,
	flag:false,
	from:51,
	init:function(_account,_password,_upBtn,_from,_verifycode,_verifycodeShow){
		var ipts = [_account,_password];
		XN.Entry.from = _from || XN.Entry.from;
		this.upBtn = FWK.elementQuery(_upBtn);
		this.upBtn.onclick = this.validate;
		if(_verifycode && _verifycodeShow){
			ipts.push(_verifycode);
			this.verifycode = FWK.elementQuery(_verifycodeShow,"setStyle");
			this.verifycode.onclick = this.reVerifycode;
		}
		this.ipts = FWK.elementQuery(ipts);
		FWK.Elements.setAtt.call(this.ipts,{onkeyup:this.validate,onfocus:this.validate});
		if(this.verifycode){
			this.reVerifycode();
		}
		ipts = null;delete ipts;
	},
	setEmptyIpts:function(){
		FWK.Elements.setAtt.call(this.ipts,"value","");
	},
	setInfo:function(){
		this.info = FWK.elementQuery(arguments[0],'setAtt');
	},
	setInterval:function(_interval){
		this.interval = _interval*1000;
	},
	reVerifycode:function(){
		if(XN.Entry.ipts.length<3)return;
		setTimeout(function(){
			XN.Entry.verifycode.setStyle({
				backgroundImage: "url(" + XN.Entry.urls.verifycode + "&kk=" + Date.parse(new Date()) + ")"
			});
			XN.Entry.ipts[2].value = "";
		},10);
	},
	validate:function(_ev){
		_ev = _ev || window.event;
		if(this==XN.Entry.upBtn || this==XN.Entry || (_ev && _ev.keyCode=="13")){
			//提交
			if(XN.Entry.info){
				for(var i=0;i<XN.Entry.ipts.length;i+=1){
					if(!XN.Entry.validateOne(i,XN.Entry.ipts[i].value)){
						return ;
					}
				}
				XN.Entry.info.innerHTML = XN.Entry.loginInfo[0];
				XN.Entry.login();
			}
			else{
				XN.Entry.alerts = null;XN.Entry.alerts = [];
				for (var i = 0; i < XN.Entry.ipts.length; i += 1) {
					var pushStr = XN.Entry.validateOne(i,XN.Entry.ipts[i].value);
					if(pushStr!=true && pushStr!=""){
						XN.Entry.alerts.push(pushStr);
					}
				}
				if(XN.Entry.alerts.length>0){
					alert("以下原因导致提交失败\t\t\t\t\n" + XN.Entry.alerts.join("\n"));
				}
				else{
					//提交
					XN.Entry.login();
				}
			}
		}
		else if(this==XN.Entry.ipts[0]){
			//帐号
			XN.Entry.validateOne(0,this.value);
		}
		else if(this==XN.Entry.ipts[1]){
			//密码
			XN.Entry.validateOne(1,this.value);
		}
		else if(XN.Entry.ipts[2] && this==XN.Entry.ipts[2]){
			//验证
			XN.Entry.validateOne(2,this.value);
		}
		else {
			alert("验证传入参数不正确");
		}
	},
	validateOne:function(_type,_value){
		if(this.info){
			var info = this.info;
			if(_value==""){
				//try{var loginType = XN.Entry.$loginType();}catch(e){var loginType = 2}
				info.innerHTML = this.emptys[_type];
				this.ipts[_type].focus();
				return false;
			}
			else if(!this.regExp[_type].test(_value.trim())){
				info.innerHTML = this.errors[_type];
				this.ipts[_type].focus();
				return false;
			}
			else{
				info.innerHTML = "&nbsp;";
				return true;
			}
		}
		else{
			if(!this.regExp[_type].test(_value.trim())){
				this.ipts[_type].focus();
				return this.errors[_type];
			}
			return true;
		}
	},
	loginTypeChange:function(){
		//XN.Entry.validate.call(XN.Entry.ipts[0]);
	},
	repeat:0,
	//登录提交
	login:function(){
		if(XN.Entry.flag)return;
		XN.Entry.flag = true;
		//try{var loginType = XN.Entry.$loginType();}catch(e){var loginType = XN.Entry.mailRegExp.test(XN.Entry.ipts[0].value.trim())?"1":"0"}
		FWK.Util.AjaxList.open(XN.Entry.urls.login,"POST",{/*loginType:loginType,*/account:XN.Entry.ipts[0].value.trim(),password:XN.Entry.ipts[1].value.trim(),from:this.from,verify:XN.Entry.ipts[2]?XN.Entry.ipts[2].value.trim():"snailgame",repeat:this.repeat},XN.Entry.loginComplete);
		try{XN.Entry.$login();}catch(e){}
	},
	loginComplete:function(_xml){
		try{var ret = _xml?XN.checkResponse(_xml):{code:-1,des:"登录路径不对"};}catch(e){var ret = {code:-1,des:"登录路径不对"};}
		if(ret==true){
			XN.Entry.loginXML = _xml;
			try{XN.Entry.$loginComplete();}catch(e){}
			if(XN.Entry.interval && XN.Entry.interval>0){
				XN.Entry.waiting = setTimeout(XN.Entry.loginValidate,XN.Entry.interval);
			}
			try{XN.XNail.onLineComplete(_xml,XN.Entry.formUrl[XN.Entry.from]||0)}catch(e){} //3D
			if(XN.Entry.info){
				try{XN.Entry.info.innerHTML = XN.Entry.loginInfo[1];}catch(e){}
			}
		}
		else{
			try{XN.Entry.$noLogin(ret);}catch(e){}
			switch(String(ret.code)){
				case "4005":
					//XN.Entry.showErr(":: 你的通行证尚未激活 ","http://register." + XN.doMain + "/index.html");
					//break;
				case "4039":
				case "4040":
				case "4045":
					try{XN.Entry.$setActive(ret);}catch(e){}
					//top.document.location.replace("http://register." + XN.doMain + "/index.html?isOld=y");
					//XN.Entry.showErr(":: "+ret.des+" ","http://register." + XN.doMain + "/index.html");
					break;
				case "4038":
					//XN.Entry.showErr(":: "+ret.des+" ","http://register." + XN.doMain + "/active.html");
					//top.document.location.replace("http://register." + XN.doMain + "/reapply.html");http:
					top.document.location.replace("http://register." + XN.doMain + "/reapply.html?isOld=y");
					break;
				default:
					XN.Entry.showErr(":: "+ret.des);
					break;
			}
			XN.Entry.reVerifycode();
		}
		XN.Entry.flag = false;
		ret = null;delete ret;
	},
	showErr:function(_msg,_url){
		if(!_url){
			if(XN.Entry.info){
				XN.Entry.info.innerHTML = "<span style='color:red'>"+_msg+"</span>";
			}
			else{
				alert(_msg);
			}
			return ;
		}
		if(XN.Entry.info){
			XN.Entry.info.innerHTML = "<span style='color:red'>"+_msg+"[<a style='color:red' target='_blank' href='"+_url+"'>激活第1虚拟通行证</a>]</span>";
		}
		else{
			if(confirm(_msg + "\n现在就去激活第1虚拟通行证")){
				window.open(_url,"d1xn");
			}
		}
	},
	loginValidate:function(_type,_from){
		_type = _type || 0;
		XN.Entry.from = _from || XN.Entry.from;
		if (XN.Entry.flag) {
			if(XN.Entry.interval && XN.Entry.interval>0){
				XN.Entry.waiting = setTimeout(XN.Entry.loginValidate,XN.Entry.interval);
			}
			return;
		}
		XN.Entry.flag = true;
		FWK.Util.AjaxList.open(XN.Entry.urls.identity[_type],"POST",null,XN.Entry.loginValidateComplete);
	},
	loginValidateComplete:function(_xml){
		var ret = _xml?XN.checkResponse(_xml):{code:-1,des:"验证路径不对"};
		XN.Entry.flag = false;
		if (ret == true) {
			if(!XN.Entry.loginXML){
				XN.Entry.loginXML = _xml;
				try{XN.Entry.$loginComplete();}catch(e){}
				try{XN.XNail.onLineComplete(_xml,XN.Entry.formUrl[XN.Entry.from]||0)}catch(e){} //3D
//				setTimeout(function() {XN.XNail.onLineComplete(_xml,XN.Entry.formUrl[XN.Entry.from]||0)},0);
			}
			if(XN.Entry.interval && XN.Entry.interval>0){
				XN.Entry.waiting = setTimeout(XN.Entry.loginValidate,XN.Entry.interval);
			}
		}
		else{
			if(XN.Entry.loginXML){
				if (ret.code != -1) {
					XN.Entry.loginXML = null;
					try {
						XN.Entry.$logoutComplete();
					} catch (e) {
					}
				}
			}
			else{
				try{XN.Entry.$noLogin(ret);}catch(e){}
			}
		}
		ret = null;delete ret;
	},
	logout:function(){
		if (XN.Entry.flag) return;
		XN.Entry.flag = true;
		FWK.Util.AjaxList.open(XN.Entry.urls.logout,"POST",null,XN.Entry.logoutComplete);
	},
	logoutComplete:function(_xml){
		var ret = _xml?true:{code:-1,des:"登出路径不对"};
		if (ret == true) {
			try{XN.Entry.$logoutComplete(_xml.code=="1402"?false:true);}catch(e){}
			XN.Entry.loginXML = null;
		}
		else{
			//alert(ret.des);
		}
		XN.Entry.clear();
		XN.Entry.flag = false;
		ret = null;delete ret;
	},
	clear:function(){
		if(XN.Entry.waiting){
			clearTimeout(XN.Entry.waiting);
		}
	}
}